// import csApi from "../../../apis/uc/index.mjs";
import tagApi from "../../../apis/rc/tag.mjs";
import ucApi from "@rc/apis/uc/index.mjs";
import TableData from "./components/table-data.vue";
import DialogItem from "./components/dialog.vue";
export default {
	name: "resourceTag",
	components: {
		TableData,
		DialogItem
	},
	data() {
		return {
			// 表格筛选条件
			queryForm: [
				{
					type: "input",
					label: "标签名称",
					key: "name",
					property: {
						type: "input",
						placeholder: "请输入标签名称"
					},
					default: ""
				},
				{
					type: "treeSelect",
					label: "项目",
					key: "projectId",
					getData: function getData() {
						return USSD.getProjectTree();
					},
					property: {
						props: {
							label: "name",
							value: "id",
							children: "children"
						}
					},
					default: ""
				},
				{
					type: "filterSelect",
					label: "标签状态",
					key: "availableState",
					getData: function getData() {
						return {
							"": "全部",
							Y: "启用",
							N: "禁用"
						};
					},
					property: {
						filterable: false
					},
					default: ""
				},
				{
					type: "filterSelect",
					label: "标签属性",
					key: "labelProperty",
					getData: function getData() {
						return {
							"": "全部",
							R: "单选",
							C: "多选"
						};
					},
					property: {
						filterable: false
					},
					default: ""
				},
				{
					type: "filterSelect",
					label: "应用范围",
					key: "applyScope",
					getData: function getData() {
						return {
							"": "全部",
							C: "课件",
                            Q: "试题"
						};
					},
					property: {
						filterable: false
					},
					default: ""
				},
				{
					type: "remoteSelect",
					label: "创建人",
					key: "createUserId",
					property: {
						placeholder: "创建人姓名",
						clearable: true,
						options: [],
						props: {
							label: "label",
							value: "value"
						},
						remoteMethod(item, query) {
							if (StringUtil.hasLength(query)) {
								ucApi.queryUserList({
									data: { userName: query },
									success: ({ data }) => {
										item.options = data.map((i) => ({
											label: i.userName + " - " + i.schoolName,
											value: i.userId
										}));
									}
								});
							} else {
								item.options = [];
							}
						}
					}
				}
			],
			tableData: [], //表单数据
			searchLoading: false, //
			//分页状态
			pagination: {
				pageIndex: 1, //当前页
				pageSize: 10, //每页条数
				pageCount: 1 //共多少页
			},
			tableDataItem: {}, //点击详情或者编辑时该行的信息
			dialogFlag: false, //弹窗是否显示
			tableDataIndex: 0, //表格索引
			query: {}, //查询参数
			tableDataItemIndex: 0 //查看时的表格索引
		};
	},
	methods: {
		//初始化查找表单
		onQueryForm() {
			this.pagination.pageIndex = 1;
			this.query = this.query = this.$refs.queryForm.getResult();
			this.queryTableData();
		},
		//跳转至添加标签页
		onAddTag() {
			nlapp.PageUtil.navigateToOnly("/rc/tag/add");
		},
		//查找表单
		queryTableData() {
			this.tableData = [];
			let queryParams = {
				name: this.query.name,
				projectId: this.query.projectId,
				availableState: this.query.availableState,
				labelProperty: this.query.labelProperty,
				applyScope: this.query.applyScope,
				createUserId: this.query.createUserId,
				pageIndex: PageUtil.getStartIndex(this.pagination),
				pageSize: this.pagination.pageSize
			};
			this.searchLoading = true;
			tagApi.getResourceTagList({
				data: queryParams,
				success: (res) => {
					this.pagination.pageCount = res.count;
					this.tableData = res.data;
				},
				complete: () => {
					this.searchLoading = false;
				}
			});
		},
		//点击查看详情时
		onViewDetail(id) {
			this.tableDataItemIndex = id;
			this.dialogFlag = true;
		},
		//关闭详情
		closeDetail() {
			this.tableDataItemIndex = "";
			this.dialogFlag = false;
		},
		//点击停用
		changeItemState(index) {
			let params = this.tableData[index];
			this.tableData.splice(index, 1);
			params.availableState = "N";
			this.tableData.push(params);
			this.onQueryForm();
		},
		//启用
		openChangeItemState(index) {
			let params = this.tableData[index];
			params.availableState = "Y";
			this.onQueryForm();
			this.$set(this.tableData, index, params);
		},
		//分页
		changesListPage(data) {
			this.pagination = data;
			this.queryTableData();
		}
	},
	created() {
		nlapp.PermissUtil.checkPagePermiss("rc/tag/list-list");
	},
	mounted() {
		this.onQueryForm();
	},
	activated() {
		//切换标签时，保留表格数据
		this.queryTableData();
	}
};
